# -*- mode: Awk; -*-  vim: set filetype=awk :
#
# This file is part of GLEE, copyright Tim Menzies 2010, 
# tim@menzies.us  
# 
# GLEE is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GLEE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GLEE.  If not, see <http://www.gnu.org/licenses/>.

Swap Data with file,before,switch,klassId,fs,read,after,has,data,rank,name,goal,range,number

 function data(Data:d,   todo,after,n,x) { 
     todo       = d_before
     IGNORECASE = 1
     while((getline x < d_file) > 0 ) {
         n++
         if ($1 ~ /^$/) continue
         &todo(d_)
         if ( &d_switch() ) {
             todo = d_after; FS = d_fs
         }
         assert(n,"[" d_file "] not found") }
 }
 function arff(file,Data:d) { 
     d_file     = file
     d_fs       = ","
     d_before   = "arff_b4"
     d_switch   = "arff_switch"
     d_after    = "data_read"
     d_relation = "relation"
     data(d_)
 }
 function arff_b4(Data:d) {
     if ($0 ~ /^@relation/) 
         d_spec["relation"]=$2
     else 
         if ($0 ~ /^@attribute/) {
             d_spec["goal"] = d_name[$2] = ++d_spec["col"]
             d_number[$2]   = /^(integer|real|continous)$/ }
 }
 function csv_b4(Data:d,   col) {
     for(col=1;col<=NF;col++) {
        d_name[$col] = ++d_spec["col"]
        d_number[$2] = $1 ~ /\$/ }
     d_spec["goal"] = NF
 }
 function arff_switch(Data:d) { return /^@data / }
 function csv_switch(Data:d)  { return 1 }

 function data_after(Data:d,  row,col) {
     gsub(/%.*/,  "")
     gsub(/[ \t]/,"")
     row = ++d_spec["row"]
     for(col=1;col<=NF;col++) { 
         d_data[row,col] = $col
         if (d_number[col])
             minmax($col,d_) }
 }
 function minmax(col,x,Data:d) {
     d_max[col]= x > d_max[col] ? x : d_max[col]
     d_min[col]= x < d_max[col] ? x : d_min[col] 
 }
 function csv(file,   Data:d) { 
     d_file     = file
     d_fs       = ","
     d_b4       = "csv_b4"
     d_switch   = "csv_switch"
     d_after    = "data_read"
     d_relation = "relation"
     data(d_)
 }

 function

Uses
====

@include lib.glee

Author
======

Tim Menzies
